package com.dai.chun.dbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * 
 * @author jiangchao
 */
public class DBManager {
	private static DBManager dbc;
	private static ComboPooledDataSource cpds;

	private DBManager() {
		cpds = new ComboPooledDataSource("intergalactoApp");
	}

	public static DBManager getInstance() {
		if (dbc == null) {
			System.out.println(new Date().toString()+"********get DBManager instance********");
			dbc = new DBManager();
			return dbc;
		} else {
			return dbc;
		}
	}

	public Connection getConnection() throws SQLException {
		System.out.println(new Date().toString()+"********get connection********");
		return cpds.getConnection();
	}

	public static ComboPooledDataSource getDataSource() throws SQLException {
		return cpds;
	}

	// 关闭连接
	public void closeConn(Connection conn, Statement stmt, ResultSet rs) {
		System.out.println(new Date().toString()+"********close connection********");
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	// 关闭连接
	public void closeConn(Connection conn, Statement stmt) {
		System.out.println(new Date().toString()+"********close connection********");
		try {
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void close(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn = null;
		}
	}

	public void close(Statement stmt) {
		try {
			if (stmt != null) {
				stmt.close();
				System.out.println(new Date().toString()+"********close connection********");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			stmt = null;
		}
	}

	public void close(ResultSet set) {
		try {
			if (set != null) {
				set.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			set = null;
		}
	}

	public void setAutoCommit(Connection conn, Boolean vlaue) {
		try {
			conn.setAutoCommit(vlaue);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void commit(Connection conn) {
		try {
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void rollback(Connection conn) {
		try {
			conn.rollback();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Statement createStmt(Connection conn) {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stmt;
	}

	public PreparedStatement createPRPstmt(Connection conn, String sql) {
		PreparedStatement PRPstmt = null;
		try {
			PRPstmt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return PRPstmt;
	}

	public ResultSet getReSet(Connection conn, String sql) {
		ResultSet reSet = null;
		try {
			reSet = conn.createStatement().executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return reSet;
	}
}
